package mvc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import geographer.Database;
import geographer.GeographicRegion;

/**
 * Project #4
 * CS 2334, 010
 * 04/02/2014
 * <P>
 * This class represents the controller for the program. It is responsible for all functionalities
 * behind the scenes.
 * </P>
 * @version 1.0
 */
public class RegionController 
{
	/* The Region Model */
	RegionModel model;
	
	/* The Selection View */
	SelectionView view;
	
	/**
	 * The constructor for the controller.
	 */
	public RegionController(){}//end constructor
	
	public static void saveData(SelectionView view)
	{
		int choice = JOptionPane.showConfirmDialog(view, "Are you sure you want to output available information?");
		   if(choice == JOptionPane.YES_OPTION)
		   {
			   try{
			   Database.outputBinary();
			   }
			   catch(IOException e)
			   {
				   System.out.println("There was a problem writing the file.");
			   }
		   }
	}
	
	public static void exportData(SelectionView view)
	{
		JFileChooser chooser1 = new JFileChooser();
		chooser1.setMultiSelectionEnabled(true);
		int option1 = chooser1.showOpenDialog(view);
		if(option1 == JFileChooser.APPROVE_OPTION)
		{
			File[] files = chooser1.getSelectedFiles();
			try{
				for(File file : files)
				{
					Database.print(view.getModel().getKeys(Database.Region.CONTINENT), file.getPath());
					Database.print(view.getModel().getKeys(Database.Region.COUNTRY), file.getPath());
					Database.print(view.getModel().getKeys(Database.Region.CITY), file.getPath());
					Database.print(view.getModel().getKeys(Database.Region.PLACE), file.getPath());
					Database.print(view.getModel().getKeys(Database.Region.POINT), file.getPath());
				}
			}catch(IOException e)
			{
				System.out.println("There was a problem writing to the file(s).");
			}
		}
	}
	
	/**
	 * Imports data from a specified text file.
	 * @param fileName  The location of the file.
	 * @param region   The region that is being added.
	 */
	public static void loadData(SelectionView view){
		try{
			Database.binaryScan();
			view.getModel().processEvent(new ActionEvent(view,0,"Import"));
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
			catch(ClassNotFoundException e)
			{
				e.printStackTrace();
			}
		
	}//end importData
	
	/**
	 * Loads data from a text file.
	 * @param fileName  The location of the file.
	 * @param region   The region that is being added.
	 */
	public static void importData(SelectionView view){
		JFileChooser chooser = new JFileChooser();
		 chooser.setMultiSelectionEnabled(true);
			 int option = chooser.showOpenDialog(view);
			 if(option == JFileChooser.APPROVE_OPTION)
			 {
				 File[] file = chooser.getSelectedFiles();
				 try{
					 Database.scan(file[1].getPath(), Database.Region.CONTINENT);
					 Database.scan(file[2].getPath(), Database.Region.COUNTRY);
					 Database.scan(file[0].getPath(), Database.Region.CITY);
					 Database.scan(file[3].getPath(), Database.Region.PLACE);
					 Database.scan(file[4].getPath(), Database.Region.POINT);
					 view.getModel().processEvent(new ActionEvent(view,0,"Load"));
				 }
				 catch(IOException e)
				 {
					 System.out.println("There was a problem opening the files.\n");
					 e.printStackTrace();
				 }
			 }
	}//end loadData
	
	public void setView(SelectionView view)
	{
		this.view = view;
		for(JMenuItem item : this.view.getJMenuItems())
		{
			item.addActionListener(view.new MenuListener());
		}
		
		for(JButton button : this.view.getButtons())
		{
			button.addActionListener(view.new ButtonListener());
		}
	}
	
	/**
	 * Sets the model
	 * @param model	The model to be set
	 */
	
	public void setModel(RegionModel model)
	{
		this.model = model;
	}
	
	//TODO: Listeners
}
